package cam.School.Services;

import java.io.IOException;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;

import javax.ws.rs.Consumes;

import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;

import cam.School.DAOException.DAOException;
import cam.School.DataAccessObject.DAOFactory;
import cam.School.DataAccessObject.ImageDAO;
import cam.School.DataTransfertObject.CallResult;
 
@Path("/fileServerService")
public class FileServerService {	
	@POST
	@Path("/uploadFile")	
	@Consumes(MediaType.MULTIPART_FORM_DATA)
	@Produces(MediaType.APPLICATION_JSON)
	public Response uploadFile(@FormDataParam("file") InputStream uploadedInputStream,
			@FormDataParam("file") FormDataContentDisposition fileDetail,
			@FormDataParam("objectId") long objectId) throws URISyntaxException, Exception, DAOException, IOException {
						
		if(fileDetail.getFileName()!=""){
			String fileServerLocation = "C:\\Users\\Claude\\Desktop\\CamSchool\\FileServer\\";		
			String uploadedFileLocation = fileServerLocation + fileDetail.getFileName();
			// save it		
			long saveResultCode = writeToFile(uploadedInputStream, uploadedFileLocation).getResultCode();
			
			if(new Integer((int)saveResultCode).equals(new Integer(200))){
				DAOFactory javabase = DAOFactory.getInstance("javabase.jdbc");		
			
				ImageDAO imageDAO = javabase.getPictureDAO();
				long resultCode = imageDAO.upload(objectId, uploadedFileLocation).getResultCode();
				
				if((new Integer((int)resultCode).equals(new Integer(406))) == false){
					return Response.status(Response.Status.OK).entity(resultCode).build();						
				}				
				return Response.status(Response.Status.UNAUTHORIZED).entity(resultCode).build();
			}
		}
		return Response.status(Response.Status.UNAUTHORIZED).entity("no file selected").build();		 
	}
	
	// save uploaded file to new location
		private CallResult writeToFile(InputStream uploadedInputStream,
			String uploadedFileLocation) {
	 
			try {
				OutputStream out = new FileOutputStream(new File(
						uploadedFileLocation));
				int read = 0;
				byte[] bytes = new byte[1024];
	 
				out = new FileOutputStream(new File(uploadedFileLocation));
				while ((read = uploadedInputStream.read(bytes)) != -1) {
					out.write(bytes, 0, read);
				}
				out.flush();
				out.close();
				return new CallResult(200, "save succefully");
			} catch (IOException e) { 
				return new CallResult(406, e.toString());
			} 
		}
	
	@GET
	@Path("/getFile")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getFile(@QueryParam("belongTo")long belongToObjectId) throws DAOException, IOException {
		DAOFactory javabase = DAOFactory.getInstance("javabase.jdbc");
		ImageDAO PictureDAO = javabase.getPictureDAO();	
		  
		return Response.ok(PictureDAO.getFileById(belongToObjectId)).build();
	}
	
	
	@POST
	@Path("/updateEmployeeFile")	
	@Consumes(MediaType.MULTIPART_FORM_DATA)
	@Produces(MediaType.APPLICATION_JSON)
	public Response updateEmployeeFile(@FormDataParam("file") InputStream uploadedInputStream,
			@FormDataParam("file") FormDataContentDisposition fileDetail,
			@FormDataParam("objectId") long objectId) throws URISyntaxException, Exception, DAOException, IOException {
						
		if(fileDetail.getFileName()!=""){
			String fileServerLocation = "C:\\Users\\Claude\\Desktop\\CamSchool\\FileServer\\";		
			String uploadedFileLocation = fileServerLocation + fileDetail.getFileName();
			// save it		
			long saveResultCode = writeToFile(uploadedInputStream, uploadedFileLocation).getResultCode();
			
			if(new Integer((int)saveResultCode).equals(new Integer(200))){
				DAOFactory javabase = DAOFactory.getInstance("javabase.jdbc");		
			
				ImageDAO imageDAO = javabase.getPictureDAO();
				long resultCode = imageDAO.updateEmployeeImage(objectId, uploadedFileLocation).getResultCode();
				
				if((new Integer((int)resultCode).equals(new Integer(406))) == false){
					return Response.status(Response.Status.OK).entity(resultCode).build();						
				}				
				return Response.status(Response.Status.UNAUTHORIZED).entity(resultCode).build();
			}
		}
		return Response.status(Response.Status.UNAUTHORIZED).entity("no file selected").build();		 
	}
}
